<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
    :immediate-check="false"
  >
    <!-- <van-cell
      
      :title="item.content"
    ></van-cell> -->
    <comment-item v-for="(item, index) in list"
      :key="index"
      :comment="item"
      @reply-click="$emit('reply-click',$event)"/>
  </van-list>
</template>
<script>
import { getComments } from "@/api/comment";
import CommentItem from "./comment-item.vue";
export default {
  name: "CommentList",
  components: {
      CommentItem
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true,
    },
    list:{
      type:Array,
      default:()=>[]
    },
    type:{
      type:String,
      validator(value){
        return ["a","c"].includes(value)
      },
      default:"a"
    }
  },

  data() {
    return {
      // list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      limit: 10,
      error: false,
    };
  },
  created() {
    this.loading=true
    this.onLoad();
  },

  methods: {
    async onLoad() {
      try {
        const { data } = await getComments({
          type: this.type,
          source: this.source.toString(),
          offset: this.offset,
          limit: this.limit,
        });
        const { results } = data.data;
        this.list.push(...results);
     this.$emit('onload-success', data.data);
        this.loading = false;
        if (results.length) {
          this.offset = data.data.last_id;
        } else {
          this.finished = true;
        }
      } catch (error) {
        console.log(error);
        this.error = true;
        this.loading = false;
      }
    },
  },
};
</script>